package com.ce.pms.renovation.web;

import java.math.BigDecimal;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.renovation.service.IFlowManageService;
import com.ce.pms.renovation.service.IInspectionService;
import com.ce.pms.renovation.service.IRenovateChargeService;
import com.ce.pms.renovation.service.IRenovateManageService;
import com.ce.pms.renovation.service.IRenovateSupervisionService;
import com.ce.pms.renovation.vo.DepositAccountingApprovalVO;
import com.ce.pms.renovation.vo.DepositAccountingListVO;
import com.ce.pms.renovation.vo.DepositAccountingQueryVO;
import com.ce.pms.renovation.vo.DepositAccountingVO;
import com.ce.pms.renovation.vo.InspectionRequisitionVO;
import com.ce.pms.renovation.vo.ReinspectionVO;
import com.ce.pms.renovation.vo.RenovateChargeItemVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;
import com.sitechasia.webx.core.support.Page;

/**
 * 保证金退还
 * @author 黄彦军
 *
 */
public class DepositAccountingAction extends PmsBaseAction {

	private  IRenovateChargeService renovateChargeService;

	private IBuildingService buildingService;

	private ISysrefcodeService sysrefcodeService;

	private IInspectionService inspectionService;

	private IFlowManageService flowManageService;

	private IRenovateSupervisionService renovateSupervisionService;

	private	IRenovateManageService renovateManageService;


	/**
	 * 功能描述:导航到主页面  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateMain() {

		setBuildingList();//缓存楼栋
		setApprovalStateList();//审批状态
		setReturnTermList();//缓存退还期限

		return "depositAccountingMain";
	}

	/**
	 * 功能描述:导航到空列表(第一次加载 )  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateDepositAccountingEmptyList(){
			setAttribute("totalRows", new Integer(0));
		return "depositAccountingList";
	}

	/**
	 * 功能描述: 导航到高级查询页面 <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateDepositAccountingSearch(){

		setBuildingList();//缓存楼栋
		setApprovalStateList();//审批状态
		setFeeStateList();//费用状态

		return "depositAccountingSearch";
	}



	/**
	 * 功能描述:缓存查询条件  <br>
	 * @param queryVO
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public void setDepositAccountingSearchCondition(@Read DepositAccountingQueryVO queryVO){

		setAttribute("depositAccountingSearchCondition", queryVO,Scope.SESSION);
	}

	/**
	 * 功能描述: 获取查询条件 <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public DepositAccountingQueryVO getDepositAccountingSearchCondition(){

		DepositAccountingQueryVO queryVO = (DepositAccountingQueryVO) getAttribute("depositAccountingSearchCondition");

		return queryVO == null ? new DepositAccountingQueryVO() : queryVO;
	}

	/**
	 * 功能描述:查询列表  <br>
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String queryDepositAccounting(
			@Read(key = "ec_crd",defaultValue="15")	int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){

			DepositAccountingQueryVO queryVO = getDepositAccountingSearchCondition();
			queryVO.setPageNo(pageNo);
			queryVO.setPageSize(pageSize);
			queryVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));

			Page page = renovateChargeService.queryDepositAccountings(queryVO);
			Long totalRows = page.getTotalCount();
			List<DepositAccountingListVO> depositList = page.getResult();
			BigDecimal countSum = new BigDecimal(0);
			for (DepositAccountingListVO listVO : depositList) {
				if(!BlankUtil.isBlank(listVO.getReturnSum())){
					BigDecimal temp = listVO.getReturnSum();
					countSum = countSum.add(temp);
				}
			}

			setAttribute("countSum", countSum);
			setAttribute("depositList", depositList);
			setAttribute("totalRows",totalRows.intValue());
			setAttribute("ec_p",pageNo );

		return "depositAccountingList";
	}

	/**
	 * 功能描述: 检查是否可以进行保证金退还核算 <br>
	 * @param inspectionRequistionId
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-30  <br>
	 */
	public void checkDeppositAccounting(@Read(key="licenceId")Long licenceId){
		DepositAccountingVO accountingVO = renovateChargeService.getDepositAccounting(licenceId);
		if(null == accountingVO.getApprovalState()
			|| SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_NEW.equals(accountingVO.getApprovalState())
			||SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_NO.equals(accountingVO.getApprovalState())){
			this.renderText("true");
		}else{
			this.renderText("false");
		}
	}

	/**
	 * 功能描述:导航到补退核算 <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateCountDeposit(@Read (key="licenceId")Long licenceId){

		DepositAccountingVO accountingVO = renovateChargeService.getDepositAccounting(licenceId);
		setAttribute("accountingVO", accountingVO);
		return "countDeposit";
	}

	/**
	 * 功能描述:保证金退还核算  <br>
	 * @param accountingVO
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-30  <br>
	 */
	public void depositAndAmercement(@Read DepositAccountingVO accountingVO){

		accountingVO.setCreateId(getUserId());
		accountingVO = renovateChargeService.countDepositAndAmercement(accountingVO);

		this.renderText(accountingVO.getKeyword().toString());
	}

	/**
	 * 功能描述:检查是否可以进行提交操作  <br>
	 * @param licenceId
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-31  <br>
	 */
	public void checkSubmit(@Read (key="licenceId") Long licenceId){
		DepositAccountingVO accountingVO = renovateChargeService.getDepositAccounting(licenceId);
		if (BlankUtil.isBlank(accountingVO)){
			this.renderText("false");
		}else if (SysrefcodeDO.RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE_NEW.equals(accountingVO.getApprovalState())){
			this.renderText("true");
		}else{
			this.renderText("false");
		}
	}

	/**
	 * 功能描述: 提交 <br>
	 * @param depositAccountingId
	 * Create author:黄彦军 <br>
	 * Create on:2009-1-6  <br>
	 */
	public void submitInfo(@Read (key="depositAccountingId")Long depositAccountingId,
			@Read(key="licenceId")Long licenceId){

		if(BlankUtil.isBlank(depositAccountingId)){
			DepositAccountingVO accountingVO = renovateChargeService.getDepositAccounting(licenceId);
			depositAccountingId = accountingVO.getKeyword();
		}

		List list =	sysrefcodeService.querySysrefCodesByCode("RM_DEPOSIT_APP_EMPLOYEE");
		SysrefcodeDO sysrefcodeDO = (SysrefcodeDO) list.get(0);
		Long employeeId = Long.valueOf( sysrefcodeDO.getValue());

		flowManageService.submitDepositAccounting(depositAccountingId,employeeId, getUserId());

	}

	/**
	 * 功能描述: 检查是否可以进行审批 <br>
	 * @param inspectionRequistionId
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-30  <br>
	 */
	public void checkRenovateApproval(@Read (key = "depositAccountingId")Long depositAccountingId){
		try {
			flowManageService.isAllowApproveDepositAccounting(getUserId(),depositAccountingId);
			this.renderText("true");
		} catch (BusinessException e) {
			this.renderText(super.getLocaleMessage(e.getMessage()));
		}
	}

	/**
	 * 功能描述:导航到审批 主页面  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateRenovateApprovalMain(
			@Read (key="inspectionRequistionId")Long inspectionRequistionId,
			@Read(key="licenceId")Long licenceId,
			@Read (key="depositAccountingId")Long depositAccountingId){

		setAttribute("inspectionRequisitionId", inspectionRequistionId);
		setAttribute("licenceId", licenceId);
		setAttribute("depositAccountingId", depositAccountingId);

		return "renovateApprovalMain";
	}

	/**
	 * 功能描述:导航到审批 分帧页  <br>
	 * @param inspectionRequistionId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateRenovateApproval(@Read (key="depositAccountingId")Long depositAccountingId){

		setAttribute("depositAccountingId", depositAccountingId);
		return "renovateApproval";
	}

	/**
	 * 功能描述:保证金审批  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-30  <br>
	 */
	public void addRenovateApproval(@Read DepositAccountingApprovalVO depositAccountingAppVO){

			depositAccountingAppVO.setApprovalId(getUserId());
			depositAccountingAppVO.setCreator(getUserId());
			flowManageService.approveDepositAccounting(depositAccountingAppVO);

		this.renderText("true");
	}

	/**
	 * 功能描述:导航到装修收费列表 分帧页  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateRenovationChangeList(@Read (key="licenceId")Long licenceId){

		List<RenovateChargeItemVO> chargeItemList = renovateManageService.getRequisitionChargeItemList(licenceId);

		setAttribute("chargeItemList", chargeItemList);
		setAttribute("totalRows",chargeItemList == null ? 0 : chargeItemList.size() );

		return "renovationChangeList";
	}

	/**
	 * 功能描述:导航到基本信息 分帧页  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateRenovationInfo(@Read (key="inspectionRequisitionId")Long inspectionRequisitionId){

		InspectionRequisitionVO insReqVO =  inspectionService.getInspectionRequisition(inspectionRequisitionId);

		setAttribute("insReqVO", insReqVO);
		return "renovationInfo";
	}

	/**
	 * 功能描述:导航到违规列表 分帧页  <br>
	 * @param inspectionRequisitionId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String  navigateReinspectionList(@Read (key="licenceId")Long licenceId){

		List reinspectionList=renovateSupervisionService.getViolationsByLicenceId(licenceId);

		setAttribute("reinspectionList", reinspectionList);
		setAttribute("totalRows", reinspectionList== null ? 0 : reinspectionList.size());

		return "reinspectionList";
	}

	/**
	 * 功能描述:导航到复验列表 分帧页  <br>
	 * @param inspectionRequisitionId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 * 修改：黄腾飞
	 * 用途：重用
	 */
	public String navigateViolationList(@Read (key="inspectionRequisitionId")Long inspectionRequisitionId,@Read(key="hasDetail")@Out(key="hasDetail")String hasDetail){
		List<ReinspectionVO> reinspectionList=inspectionService.getReinspections(inspectionRequisitionId);

		setAttribute("reinspectionList", reinspectionList);
		setAttribute("totalRows",reinspectionList == null ? 0 : reinspectionList.size());
		return "violationList";
	}

	/**
	 * 功能描述:导航到退还核算详细 分帧页 <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateCountDepositDetail(@Read (key="licenceId")Long licenceId){

		DepositAccountingVO accountingVO = renovateChargeService.getDepositAccounting(licenceId);
		setAttribute("accountingVO", accountingVO);

		return "countDepositDetail";
	}

	/**
	 * 功能描述:导航到审批详细 分帧页  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateRenovateApprovalDetail(@Read (key="depositAccountingId")Long depositAccountingId){

		DepositAccountingApprovalVO approvalVO=	flowManageService.getApproveDepositAccountingBy(depositAccountingId);

		setAttribute("approvalVO", approvalVO);

		return "renovateApprovalDetail";
	}

	/**
	 * 功能描述:详细 主页面  <br>
	 * @param licenceId
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-28  <br>
	 */
	public String navigateDetailMain(@Read (key="licenceId")Long licenceId,
				@Read(key="inspectionRequisitionId")Long inspectionRequisitionId,
				@Read(key="depositAccountingId")Long depositAccountingId){

		setAttribute("licenceId", licenceId);
		setAttribute("depositAccountingId", depositAccountingId);
		setAttribute("inspectionRequisitionId", inspectionRequisitionId);
		return "detailMain";
	}

	/**
	 * 功能描述: 缓存楼栋 <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2009-1-6  <br>
	 */
	private void setBuildingList(){
		setAttribute("buildingList", buildingService.getAllBuildingList()); //加载楼栋
	}

	/**
	 * 功能描述:缓存退还期限  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2009-1-6  <br>
	 */
	private void setReturnTermList(){
		setAttribute("returnTermList",sysrefcodeService.querySysrefCodesByCode("RM_RETURN_FEE_PERIOD") );//退还期限
	}

	/**
	 * 功能描述:审批状态  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2009-1-6  <br>
	 */
	private void setApprovalStateList(){
		setAttribute("approvalStateList",sysrefcodeService.querySysrefCodesByCode("RM_DEPOSIT_ACCOUNTING_APPROVAL_STATE") );//审批状态
	}

	/**
	 * 功能描述:缓存费用状态  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2009-1-6  <br>
	 */
	private void setFeeStateList(){
		setAttribute("feeStateList",sysrefcodeService.querySysrefCodesByCode("RM_FEE_STATE") );//费用状态
	}




	//
//	/**
//	 * 功能描述: 导航到提交页 <br>
//	 * @return
//	 * Create author:黄彦军 <br>
//	 * Create on:2008-12-31  <br>
//	 */
//	public String navigateSubmitPage(@Read(key="inspectionRequistionId") @Out (key="inspectionRequistionId")Long inspectionRequistionId){
//		//setAttribute("submitList",sysrefcodeService.querySysrefCodesByCode("RM_RETURN_FEE_PERIOD") );//保证金审批人
//		//List submitList = new ArrayList();
//		SysrefcodeDO submitVO = new SysrefcodeDO();
//		submitVO.setName("张中祥");
//		submitVO.setValue("1");
//		submitVO.setRemark("客服中心");
//
//		//submitList.add(sysrefcode);
//
//		setAttribute("submitVO", submitVO);
//		//setAttribute("totalRows", submitList.size());
//
//		return "submitPage";
//	}
	/*********************************************************************************/
	public void setRenovateChargeService(
			IRenovateChargeService renovateChargeService) {
		this.renovateChargeService = renovateChargeService;
	}

	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	public void setSysrefcodeService(ISysrefcodeService sysrefcodeService) {
		this.sysrefcodeService = sysrefcodeService;
	}


	public void setFlowManageService(IFlowManageService flowManageService) {
		this.flowManageService = flowManageService;
	}

	public void setInspectionService(IInspectionService inspectionService) {
		this.inspectionService = inspectionService;
	}

	public void setRenovateSupervisionService(
			IRenovateSupervisionService renovateSupervisionService) {
		this.renovateSupervisionService = renovateSupervisionService;
	}

	public void setRenovateManageService(
			IRenovateManageService renovateManageService) {
		this.renovateManageService = renovateManageService;
	}



}
